﻿
Imports System.IO
Imports std
Imports xmlctrl

Public Class parser
    Inherits xmlparser.parser

    Public root As xmlcontrol = Nothing
    Protected _channels As list(Of pointer(Of xmlcontrol)) = Nothing
    Protected _items As list(Of pair(Of pointer(Of xmlcontrol), pointer(Of xmlcontrol))) = Nothing
    Private lastchannel As pointer(Of xmlcontrol) = Nothing

    Public Function channels() As list(Of pointer(Of xmlcontrol))
        Return _channels
    End Function

    Public Function items() As list(Of pair(Of pointer(Of xmlcontrol), pointer(Of xmlcontrol)))
        Return _items
    End Function

    Public Overloads Function parse(ByRef content As String) As Boolean
        Return MyBase.parse(content, root)
    End Function

    Protected Overrides Function generateControl(ByVal tag As String, ByVal oneline As Boolean) As xmlcontrol
        Dim rtn As xmlcontrol = Nothing
        rtn = MyBase.generateControl(tag, oneline)
        If strsame(tag, "channel", False) Then
            lastchannel = New pointer(Of xmlcontrol)(rtn)
            _channels.push_back(lastchannel)
        ElseIf strsame(tag, "item", False) Then
            _items.push_back(make_pair(New pointer(Of xmlcontrol)(rtn), lastchannel))
        End If
        Return rtn
    End Function

    Public Sub clear()
        If _channels Is Nothing Then
            _channels = New list(Of pointer(Of xmlcontrol))()
        Else
            _channels.clear()
        End If

        If _items Is Nothing Then
            _items = New list(Of pair(Of pointer(Of xmlcontrol), pointer(Of xmlcontrol)))()
        Else
            _items.clear()
        End If
    End Sub

    Public Sub New()
        MyBase.New()
        clear()
    End Sub

    Public Sub New(ByRef output As MemoryStream)
        MyBase.New(output)
        clear()
    End Sub
End Class
